package com.h3bpm.kumhosunny.purchase.controller;


import OThinker.Common.Organization.Models.User;
import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Entity.Instance.Data.InstanceData;
import com.h3bpm.base.util.AppUtility;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import com.h3bpm.kumhosunny.sample.controller.utils.DateUtil;
import com.h3bpm.kumhosunny.sample.controller.utils.NemberUtil;
import data.DataRow;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author: wjq
 * @Date: 2019/9/23 18:37
 * @Description:
 */
@Component
public class SchemeEvaluationTask extends ControllerBase {

    private static Logger log = LoggerFactory.getLogger(SchemeEvaluationTask.class);


    @Scheduled(cron = "0 */2 * * * ?")
    public void schemeEvaluationCheckout(){

        try {
            log.debug("修改方案评估多人审批校验开始================================");

            String sqlInstance = "SELECT " +
                    " a.*  " +
                    " FROM " +
                    " ot_instancecontext a " +
                    "   " +
                    " WHERE " +
                    " a.WorkflowCode = 'schemeEvaluation'  and  a.State!=4 " ;

            DataTable dataTableInstance = SqlUtil.doQuery(sqlInstance);

            if(dataTableInstance !=null  && dataTableInstance.getEntityRows()!=null && dataTableInstance.getEntityRows().size()>0) {
                for (int i = 0; i < dataTableInstance.getEntityRows().size(); i++) {
                    DataRow row = dataTableInstance.getEntityRows().get(i);


                    String sql0 = "SELECT " +
                            " a.*  " +
                            " FROM " +
                            " ot_token a " +
                            " INNER JOIN ot_instancecontext b ON b.ObjectID = a.ParentObjectID  " +
                            " WHERE " +
                            " b.WorkflowCode = 'schemeEvaluation'  " +
                            " AND ( a.Activity = 'Activity24' OR a.Activity = 'Activity25' OR a.Activity = 'Activity26' OR a.Activity = 'Activity28' )  " +
                            " and  a.ParentObjectID= '" + row.getString("ObjectID") + "' ";

                    DataTable dataTable0 = SqlUtil.doQuery(sql0);
                    if (dataTable0 != null && dataTable0.getEntityRows() != null && dataTable0.getEntityRows().size() > 0) {

                        String sql = "SELECT " +
                                " a.*  " +
                                " FROM " +
                                " ot_token a " +
                                " INNER JOIN ot_instancecontext b ON b.ObjectID = a.ParentObjectID  " +
                                " WHERE " +
                                " b.WorkflowCode = 'schemeEvaluation'  " +
                                " AND ( a.Activity = 'Activity24' OR a.Activity = 'Activity25' OR a.Activity = 'Activity26' OR a.Activity = 'Activity28' )  " +
                                " AND a.State = 0 and a.ParentObjectID= '" + row.getString("ObjectID") + "' ";

                        DataTable dataTable = SqlUtil.doQuery(sql);

                        if (dataTable != null && dataTable.getEntityRows() != null && dataTable.getEntityRows().size() > 0) {
                            InstanceData instanceData = new InstanceData(AppUtility.getEngine(), row.getString("ObjectID"), User.AdministratorID);
                            instanceData.getBizObject().setItem("isTj", "未完成");
                            instanceData.Submit();
                        } else {
                            InstanceData instanceData = new InstanceData(AppUtility.getEngine(), row.getString("ObjectID"), User.AdministratorID);
                            if (!(instanceData.getBizObject().getItem("isTj") != null && !"".equals(instanceData.getBizObject().getItem("isTj")) && "已完成".equals(instanceData.getBizObject().getItem("isTj")))) {
                                instanceData.getBizObject().setItem("isTj", "已完成");
                                instanceData.Submit();
                            }
                        }
                    } else {
                        InstanceData instanceData = new InstanceData(AppUtility.getEngine(), row.getString("ObjectID"), User.AdministratorID);
                          instanceData.getBizObject().setItem("isTj", "未完成");
                            instanceData.Submit();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getFunctionCode() {
        return null;
    }
}
